const commonFn = {
	getImageUrl(value, host, small) {
		if (!value) {
			return ''
		}
		let reg = /^20[0-9]{12,}\.[a-zA-Z0-9]{3,}/
		let rs = ''
		if (typeof host == 'undefined') {
			host = 'https://ourydcimage.shenghua6.cn'
		}
		let sub_year = value.substr(0, 4)
		let sub_month = value.substr(4, 2)
		let sub_date = value.substr(6, 2)
		value = value.replace(/^\/*|\/*$/g, '')
		if (reg.test(value)) {
			rs = host + '/' + sub_year + '/' + sub_month + '/' + sub_date + '/' + value
		} else {
			rs = host + '/' + value
		}
		if (typeof small !== 'undefined') {
			rs = rs + '@!100w_100h'
		}
		return rs
	},
	getTimeFormat(value, type) {
		if (!value) {
			return ''
		}
		let date
		if (value.length !== 10) {
			date = new Date(parseInt(value))
		} else {
			date = new Date(parseInt(value * 1000))
		}
		let Y = date.getFullYear() + '-'
		let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
		let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '
		let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
		let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
		let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
		if (type == 'YMD') {
			return Y + M + D
		} else {
			return Y + M + D + h + m + s
		}
	},
	urlTobase64(url) {
		return new Promise(function(resolve, reject) {
			uni.getFileSystemManager().readFile({
				filePath: url, //选择图片返回的相对路径
				encoding: 'base64', //编码格式
				success: res => { //成功的回调
					let base64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符，在页面无法显示的哦
					resolve(base64)
				},
				fail: (e) => {
					reject(e)
				}
			})
		})
	},
	/**
	 * 时间戳转为多久之前
	 * @param String timestamp 时间戳
	 **/
	formatMsgTime(start, end) {
		// console.log(this.getTimeFormat(start), this.getTimeFormat(end))
		var dateTime = new Date(start);

		var year = dateTime.getFullYear();
		var month = (dateTime.getMonth() + 1 < 10 ? '0' + (dateTime.getMonth() + 1) : dateTime.getMonth() + 1);
		var day = (dateTime.getDate() < 10 ? '0' + (dateTime.getDate()) : dateTime.getDate());
		var hour = (dateTime.getHours() < 10 ? '0' + dateTime.getHours() : dateTime.getHours());
		var minute = (dateTime.getMinutes() < 10 ? '0' + dateTime.getMinutes() : dateTime.getMinutes());
		var second = (dateTime.getSeconds() < 10 ? '0' + dateTime.getSeconds() : dateTime.getSeconds());
		var now = new Date(end);
		var now_new = Date.parse(now.toDateString()); //typescript转换写法

		var milliseconds = 0;
		var timeSpanStr;

		milliseconds = now_new - start;

		if (milliseconds <= 1000 * 60 * 1) {
			timeSpanStr = '刚刚';
		} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
			timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
		} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
		} else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
		} else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
			timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
		} else {
			timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
		}
		return timeSpanStr;
	},

	getAge(birthDayTime,nowTime) {
		return Math.ceil((nowTime - birthDayTime) / 31536000000);
	},
	getAstro(month,day){
	  var s="魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
	  var arr=[20,19,21,21,21,22,23,23,23,23,22,22];
	  return s.substr(month*2-(day<arr[month-1]?2:0),2);
	}
}

export default commonFn
